/*
 *    Copyright 2021 FightingGuys Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package cn.fightingguys.kaiheila.event;

import cn.fightingguys.kaiheila.KHL;
import cn.fightingguys.kaiheila.event.EventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.events.Event;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 事件源管理
 *
 * @author running_hyk
 * @version 1.0.0
 * @since 1.0.0
 */
public class EventManager {

    protected static final Logger Log = LoggerFactory.getLogger(EventManager.class);

    private final KHL khl;
    private final CopyOnWriteArrayList<EventListener> listeners = new CopyOnWriteArrayList<>();

    public EventManager(KHL khl) {
        this.khl = khl;
    }

    public void addEventListener(EventListener listener) {
        listeners.add(listener);
    }

    public void removeEventListener(EventListener listener) {
        listeners.remove(listener);
    }

    public List<EventListener> getListeners() {
        return listeners;
    }

    public void handle(Event event) {
        if (event == null) {
            if (Log.isDebugEnabled()) {
                Log.debug("");
            }
            return;
        }
        listeners.forEach(listener -> listener.handle(khl, event));
    }

}
